﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using qiupeng.Public;
using System.IO;
using SafetyRecordData.Data.DAL;

public partial class HumanManage_Staff_list : Base
{
    Db List = new Db();
    BindDrowDownList list = new BindDrowDownList();
    public static string CountsLabel, AllPageLabel, CurrentlyPageLabel, SqlStrStart, SqlStrMid, SqlStr, SqlStrStartCount, SqlStrCount;
    public string urlstr, keystr;
    public string CreateMidSql()
    {
        string MidSql = string.Empty;
        if (Request.QueryString["key"] != null)
        {
            if (Request.QueryString["GongHao"] != null)
            {
                MidSql = MidSql + " and GongHao like '%" + List.GetFormatStr(Server.UrlDecode(Request.QueryString["GongHao"])) + "%'";
            }

            if (Request.QueryString["XingMing"] != null)
            {
                MidSql = MidSql + " and XingMing like '%" + List.GetFormatStr(Server.UrlDecode(Request.QueryString["XingMing"])) + "%'";
            }

            if (base.Request.QueryString["bmname"] != null)
            {
                MidSql = MidSql + " and yinpinbm like '%" + this.List.GetFormatStr(base.Server.UrlDecode(base.Request.QueryString["bmname"])) + "%' ";
            }
            if (base.Request.QueryString["dwname"] != null)
            {
                MidSql = MidSql + " and danweiname like '%" + this.List.GetFormatStr(base.Server.UrlDecode(base.Request.QueryString["dwname"])) + "%' ";
            }
            if (base.Request.QueryString["zhiweileibie"] != null)
            {
                MidSql = MidSql + " and zhiweileibie like '%" + this.List.GetFormatStr(base.Server.UrlDecode(base.Request.QueryString["zhiweileibie"])) + "%' ";
            }


        }

        return MidSql;

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Session["username"] == null)
        {
            this.Response.Write("<script language=javascript>alert('登陆超时！');window.parent.location = '../main_d.aspx'</script>");
            return;

        }
        if (base.Request.QueryString["BuMenId"] != null)
        {
            this.urlstr = "&BuMenId=" + base.Request.QueryString["BuMenId"].ToString();
            string sql = "select * from qp_hr_JiGou  where id='" + this.List.GetFormatStr(base.Request.QueryString["BuMenId"]) + "'";
            OleDbDataReader list = this.List.GetList(sql);
            if (list.Read())
            {
                string str = list["QxString"].ToString();
                this.keystr = "and CHARINDEX('" + str + "',(select QxString from qp_hr_JiGou where qp_hr_JiGou.id=m.YinPinBMId)) > 0 ";
            }
            list.Close();
        }
        else
        {
            this.urlstr = "";
        }
        if (!this.Page.IsPostBack)
        {
            this.BindAttribute();
            this.DataBindToGridview();
            this.Bindquanxian();
        }


    }

    public string ReturnValue(string departID)
    {
        return new DADepartMent().ReturnJgName(departID);

    }

    public void Bindquanxian()
    {
        List.QuanXianVis(IMG2, "iiii0b", Session["perstr"].ToString());
        List.QuanXianVis(ImageButton3, "iiii0e", Session["perstr"].ToString());
        List.QuanXianVis(IMG1, "iiii0i", Session["perstr"].ToString());
    }

    public void BindAttribute()
    {

        // ImageButton1.Attributes["onclick"] = "javascript:return showwait();";
        ImageButton3.Attributes["onclick"] = "javascript:return delcheck();";


        btnFirst.Attributes["onclick"] = "javascript:return showwait();";
        btnPrev.Attributes["onclick"] = "javascript:return showwait();";
        btnNext.Attributes["onclick"] = "javascript:return showwait();";
        btnLast.Attributes["onclick"] = "javascript:return showwait();";
        Button1.Attributes["onclick"] = "javascript:return chkyema();";
    }

    public void DataBindToGridview()
    {
        SqlStrStart = "select * from (select  c.ID,XingMing,GongHao,YinPinBM,YinPinBMId,YinPinZW,Zhiwu,YgGongZ,YgZhuangTai,zhiweileibie,(select a.JgName as jgname from qp_hr_jigou a,qp_hr_JiGou b where a.id=b.ParentNodesID and b.id=c.YinPinBMId) as danweiname from qp_hr_Staff c) m where 1=1  and YgZhuangTai='在职'  " + this.keystr;
        SqlStrStartCount = "select count(id) from  (select  c.ID,GongHao,XingMing,YinPinBM,YinPinBMId,YinPinZW,Zhiwu,YgZhuangTai,YgGongZ,zhiweileibie,(select a.JgName as jgname from qp_hr_jigou a,qp_hr_JiGou b where a.id=b.ParentNodesID and b.id=c.YinPinBMId) as danweiname from qp_hr_Staff c) m where 1=1  and YgZhuangTai='在职'  " + this.keystr;
        SqlStrMid = string.Empty;
        SqlStrMid = this.CreateMidSql();
        SqlStr = SqlStrStart + SqlStrMid;
        SqlStr = SqlStr + " and YinPinBMId in (" + this.Session["bmqxa"].ToString().Trim() + ")";
        SqlStrCount = SqlStrStartCount + SqlStrMid;
        this.keystr = this.keystr + " and YinPinBMId in (" + this.Session["bmqxa"].ToString().Trim() + ")";
        string Sqlsort = "";
        if (ViewState["OrderBY"] == null)
        {
            ViewState["OrderBY"] = " ORDER BY id desc ";
        }
        Sqlsort = ViewState["OrderBY"].ToString();

        string sql = SqlStr + " " + Sqlsort;
        this.GridView1.DataSource = this.List.GetGrid_Pages_not(sql);
        this.GridView1.DataBind();
        string sqlStrCount = SqlStrCount;
        CountsLabel = Convert.ToString(this.List.GetCount(sqlStrCount));
        AllPageLabel = Convert.ToString(this.GridView1.PageCount);
        CurrentlyPageLabel = Convert.ToString((int)(this.GridView1.PageIndex + 1));
        this.btnFirst.CommandName = "1";
        this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
        this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
        this.btnLast.CommandName = this.GridView1.PageCount.ToString();

    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataControlRowType itemType = e.Row.RowType;
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
            e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
        }


        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ln = (Label)e.Row.FindControl("WFNId");
            Label ll1 = (Label)e.Row.FindControl("Label1");
            Label ll2 = (Label)e.Row.FindControl("Label2");
            // Label ll3 = (Label)e.Row.FindControl("Label3");

            ll1.Text = " <a href=\"javascript:void(0)\" onclick=\"showfrom(" + ln.Text + ");\">查看</a>";

            ll2.Text = " <a href=\"javascript:void(0)\" onclick=\"updatefrom(" + ln.Text + ");\">修改</a>";
            // ll3.Text = " <a href=\"javascript:void(0)\" onclick=\"updatefrom(" + ln.Text + ");\">其他操作</a>";

            List.QuanXianVis(ll1, "iiii0c", Session["perstr"].ToString());
            List.QuanXianVis(ll2, "iiii0d", Session["perstr"].ToString());
            // List.QuanXianVis(ll3, "cccc1ag", Session["perstr"].ToString());

        }


    }
    private string CheckCbxDel()
    {
        string str = "0";
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }


    private string CheckCbxNameDel()
    {
        string str = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabNameVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }

    private string CheckCbxUpdate()
    {
        string str = string.Empty;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox Chk = (CheckBox)row.FindControl("CheckSelect");
            Label LabVis = (Label)row.FindControl("LabVisible");
            if (Chk.Checked == true)
            {
                if (str == "")
                {
                    str = LabVis.Text.ToString();
                }
                else
                {
                    str = str + "," + LabVis.Text.ToString();
                }
            }
        }
        return str;
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sql = "";

        if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
        {
            ViewState["SortDirection"] = " desc";
        }
        else
            ViewState["SortDirection"] = "";

        ViewState["OrderBY"] = " order by " + e.SortExpression + ViewState["SortDirection"];


        DataBindToGridview();
    }

    protected void PagerButtonClick(object sender, EventArgs e)
    {
        try
        {
            GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            DataBindToGridview();
        }
        catch
        {
            Response.Write("<script language='javascript'>alert('没有这么多页！');</script>");
            return;
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        try
        {
            if (GoPage.Text.Trim().ToString() == "")
            {
                Response.Write("<script language='javascript'>alert('页码不可以为空!');</script>");
            }
            else if (GoPage.Text.Trim().ToString() == "0" || Convert.ToInt32(GoPage.Text.Trim().ToString()) > GridView1.PageCount)
            {
                Response.Write("<script language='javascript'>alert('页码不是一个有效值!');</script>");
            }
            else if (GoPage.Text.Trim() != "")
            {
                int PageI = Int32.Parse(GoPage.Text.Trim()) - 1;
                if (PageI >= 0 && PageI < (GridView1.PageCount))
                {
                    GridView1.PageIndex = PageI;
                }
            }
            DataBindToGridview();
        }
        catch
        {
            DataBindToGridview();
            Response.Write("<script language='javascript'>alert('请输入有效的页码字符！');</script>");
        }
    }

    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        string CheckStr = CheckCbxDel();

        string SqlStr = "  Delete from qp_hr_Staff  where ID in (" + CheckStr + ")";
        List.ExeSql(SqlStr);

        List.InsertLog("删除人事管理[" + CheckCbxNameDel() + "]", "人事管理");

        DataBindToGridview();
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Staff_update.aspx?id=" + CheckCbxUpdate() + "");
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Staff_add.aspx");
    }

    protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Staff_show.aspx?id=" + CheckCbxUpdate() + "");
    }
}
